package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {


            String hospJsonStr = JSONObject.toJSONString(paramMap);

            Department department = JSONObject.parseObject(hospJsonStr, Department.class);

        Department departmentExist =  departmentRepository.getDepartmentByHoscodeAndDepcode(
                department.getHoscode(),department.getDepcode());
        if(departmentExist!=null){
            //更新
            department.setId(departmentExist.getId());
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);

        }else{
            //新增
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }

    @Override
    //根据医院编号,查询医院所有科室列表
    public List<DepartmentVo> findDeptTree(String hoscode) {
        ArrayList<DepartmentVo> result = new ArrayList<>();
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);
        //处理科室集合,根据大科室id进行划分,用流失计算
        //处理后结果map分析,key=bigcode,value=大科室下所有小科室集合
        Map<String,List<Department>> departmentMap = departmentList.stream().
                collect(Collectors.groupingBy(Department::getBigcode));

        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            //封装大科室信息
            DepartmentVo bigDeptVo = new DepartmentVo();
            bigDeptVo.setDepcode(entry.getKey());
            bigDeptVo.setDepname(entry.getValue().get(0).getBigname());
            //封装小科室信息
            //创建封装小科室的集合
            List<DepartmentVo> deptVoList = new ArrayList<>();
            List<Department> deptList =  entry.getValue();
            //遍历封装
            for (Department dept : deptList) {
                DepartmentVo departmentVo = new DepartmentVo();
                BeanUtils.copyProperties(dept,departmentVo);
                deptVoList.add(departmentVo);
            }
            //小科室集合存入大科室对象
            bigDeptVo.setChildren(deptVoList);
            //大科室对象存入最终返回集合
            result.add(bigDeptVo);
        }

        return result;

    }

}

